package com.ruoyi.controller.jpjs.concretecheck;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ArrayUtil;
import com.ruoyi.domain.jpjs.common.brickcheck.BrickCheck;
import com.ruoyi.domain.jpjs.common.brickcheck.BrickCheckParam;
import com.ruoyi.domain.jpjs.common.concretecheck.ConcreteCheck;
import com.ruoyi.domain.jpjs.common.concretecheck.ConcreteCheckParam;
import com.ruoyi.domain.jpjs.common.plastercheck.PlasterCheck;
import com.ruoyi.domain.jpjs.common.plastercheck.PlasterCheckParam;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.jpjs.concrete.ConcreteService;
import com.ruoyi.service.jpjs.concretecheck.ConcretecheckService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.util.concretecheck.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author C.fw
 * @explain 混凝土实测报表控制层
 * @since 2020/8/15 15:43
 */
@Controller
@RequestMapping("/concreteCheckManage")
public class ConcreteCheckController extends BaseController {

    private String prefix = "jpjs/concretecheck";

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ConcreteService concreteService;

    @Autowired
    private ConcretecheckService concretecheckService;

    /**
     * 混凝土报表菜单url:跳转首页面
     *
     * @return 首页面
     */
    @GetMapping()
    public String list() {
        return prefix + "/concreteCheckManage";
    }

    /**
     * 初始化:获取概要列表
     *
     * @param map
     *            前台封装参数
     * @return 分页list
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo selectConcreteCheckList(@RequestParam Map<String, Object> map) {
        startPage();
        map.put("programName", map.get("programName").toString().trim().replaceAll(",", ""));
        getUserId(map);
        List<ConcreteCheck> list = concretecheckService.selectConcreteCheckList(map);
        return getDataTable(list);
    }

    private void getUserId(@RequestParam Map<String, Object> map) {
        // 获取当前登录人对应的项目权限
        // 获取当前的用户信息
        SysUser currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户ID
        Long userId = currentUser.getUserId();
        map.put("userId",userId);
    }

    /**
     * 初始化:获取概要列表
     *
     * @param map
     *            前台封装参数
     * @return 分页list
     */
    @PostMapping("/brickList")
    @ResponseBody
    public TableDataInfo selectBrickCheckList(@RequestParam Map<String, Object> map) {
        startPage();
        map.put("programName", map.get("programName").toString().trim().replaceAll(",", ""));
        // 获取当前登录人对应的项目权限
        // 获取当前的用户信息
        getUserId(map);
        List<BrickCheck> list = concretecheckService.selectBrickCheckList(map);
        return getDataTable(list);
    }

    /**
     * 初始化:获取概要列表
     *
     * @param map
     *            前台封装参数
     * @return 分页list
     */
    @PostMapping("/plasterList")
    @ResponseBody
    public TableDataInfo selectPlasterCheckList(@RequestParam Map<String, Object> map) {
        startPage();
        map.put("programName", map.get("programName").toString().trim().replaceAll(",", ""));
        // 获取当前登录人对应的项目权限
        getUserId(map);
        List<PlasterCheck> list = concretecheckService.selectPlasterCheckList(map);
        return getDataTable(list);
    }

    /**
     * 初始化:获取树组件列表
     *
     * @return 树组件list
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        return concreteService.getProgramTree();
    }

    /**
     * 初始化:获取所有工艺概要信息
     *
     * @param map
     *            前台封装参数
     * @return 分页list
     */
    @PostMapping("/listTotal")
    @ResponseBody
    public TableDataInfo selectTotalList(@RequestParam Map<String, Object> map) {
        startPage();
        map.put("programName", map.get("programName").toString().trim().replaceAll(",", ""));
        // 获取当前登录人对应的项目权限
        getUserId(map);
        List<ConcreteCheck> list = concretecheckService.selectTotalList(map);
        return getDataTable(list);
    }


    /**
     * 新增按钮:跳转新增页面
     *
     * @param mmp
     *            modelAndView
     * @param companyId
     *            工程公司ID
     * @param proId
     *            项目ID
     * @param programName
     *            项目名称
     * @return 新增页面
     */
    @GetMapping("/add")
    public String addProject(ModelMap mmp, String programName, String companyId, String proId) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", companyId);
        map.put("proId", proId);
        mmp.put("buildingNumber", concreteService.selectBuildingNumberList(map));
        mmp.put("floorNumber", sysDictDataService.selectDictDataByType("floor_number"));
        mmp.put("companyId", companyId);
        mmp.put("programId", proId);
        mmp.put("programName", programName);
        return prefix + "/concreteCheckAdd";
    }

    /**
     * 砌体-新增按钮:跳转新增页面
     * 
     * @param mmp
     *            modelAndView
     * @param programName
     *            工程公司ID
     * @param companyId
     * @param proId
     * @return
     */
    @GetMapping("/addBrickView")
    public String addBrickView(ModelMap mmp, String programName, String companyId, String proId) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", companyId);
        map.put("proId", proId);
        mmp.put("buildingNumber", concreteService.selectBuildingNumberList(map));
        mmp.put("floorNumber", sysDictDataService.selectDictDataByType("floor_number"));
        mmp.put("companyId", companyId);
        mmp.put("programId", proId);
        mmp.put("programName", programName);
        return prefix + "/brickCheckAdd";
    }

    /**
     * 砌体-新增按钮:跳转新增页面
     *
     * @param mmp
     *            modelAndView
     * @param programName
     *            工程公司ID
     * @param companyId
     * @param proId
     * @return
     */
    @GetMapping("/addPlasterView")
    public String addPlasterView(ModelMap mmp, String programName, String companyId, String proId) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", companyId);
        map.put("proId", proId);
        mmp.put("buildingNumber", concreteService.selectBuildingNumberList(map));
        mmp.put("floorNumber", sysDictDataService.selectDictDataByType("floor_number"));
        mmp.put("companyId", companyId);
        mmp.put("programId", proId);
        mmp.put("programName", programName);
        return prefix + "/plasterCheckAdd";
    }

    /**
     * 新增页面：保存混凝土按钮
     * 
     * @param concreteCheckParam
     *            混凝土实体接收类
     * @return 结果
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(ConcreteCheckParam concreteCheckParam) {
        List<ConcreteCheck> concreteCheckList = concreteCheckParam.getConcreteCheck();
        for (ConcreteCheck concreteCheck : concreteCheckList) {
            concreteCheck.setCheckPerson(ShiroUtils.getLoginName());
            concreteCheck.setCompanyId(concreteCheckParam.getCompanyId());
            concreteCheck.setProgramId(concreteCheckParam.getProgramId());
        }
        return toAjax(concretecheckService.insertConcreteCheckBatches(concreteCheckList));
    }

    /**
     * 新增页面：保存砌体按钮
     *
     * @param brickCheckParam
     *            砌体实体接收类
     * @return 结果
     */
    @PostMapping("/saveBrick")
    @ResponseBody
    public AjaxResult saveBrick(BrickCheckParam brickCheckParam) {
        List<BrickCheck> list = brickCheckParam.getBrickCheck();
        for (BrickCheck brickCheck : list) {
            brickCheck.setCheckPerson(ShiroUtils.getLoginName());
            brickCheck.setCompanyId(brickCheckParam.getCompanyId());
            brickCheck.setProgramId(brickCheckParam.getProgramId());
        }
        return toAjax(concretecheckService.insertBrickCheckBatches(list));
    }

    /**
     * 新增页面：保存抹灰按钮
     *
     * @param plasterCheckParam
     *            抹灰实体接收类
     * @return 结果
     */
    @PostMapping("/savePlaster")
    @ResponseBody
    public AjaxResult savePlaster(PlasterCheckParam plasterCheckParam) {
        List<PlasterCheck> list = plasterCheckParam.getPlasterCheck();
        for (PlasterCheck plasterCheck : list) {
            plasterCheck.setCheckPerson(ShiroUtils.getLoginName());
            plasterCheck.setCompanyId(plasterCheckParam.getCompanyId());
            plasterCheck.setProgramId(plasterCheckParam.getProgramId());
        }
        return toAjax(concretecheckService.insertPlasterCheckBatches(list));
    }

    /**
     * 编辑按钮：跳转编辑页面
     *
     * @param mmp
     *            modelAndView
     * @param checkId
     *            主键
     * @return 编辑页面
     */
    @GetMapping("/editListView")
    public String editListView(ModelMap mmp, String checkId, String programName, String companyId, String programId) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", companyId);
        map.put("proId", programId);
        mmp.put("buildingNumber", concreteService.selectBuildingNumberList(map));
        mmp.put("floorNumber", sysDictDataService.selectDictDataByType("floor_number"));
        mmp.put("checkId", checkId);
        mmp.put("programName", programName);
        return prefix + "/concreteCheckEdit";
    }

    /**
     * 编辑按钮：跳转砌体编辑页面
     *
     * @param mmp
     *            modelAndView
     * @param checkId
     *            主键
     * @return 编辑页面
     */
    @GetMapping("/editBrickView")
    public String editBrickView(ModelMap mmp, String checkId, String programName, String companyId, String programId) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", companyId);
        map.put("proId", programId);
        mmp.put("buildingNumber", concreteService.selectBuildingNumberList(map));
        mmp.put("floorNumber", sysDictDataService.selectDictDataByType("floor_number"));
        mmp.put("checkId", checkId);
        mmp.put("programName", programName);
        return prefix + "/brickCheckEdit";
    }

    /**
     * 编辑按钮：跳转抹灰编辑页面
     *
     * @param mmp
     *            modelAndView
     * @param checkId
     *            主键
     * @return 编辑页面
     */
    @GetMapping("/editPlasterView")
    public String editPlaster(ModelMap mmp, String checkId, String programName, String companyId, String programId) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", companyId);
        map.put("proId", programId);
        mmp.put("buildingNumber", concreteService.selectBuildingNumberList(map));
        mmp.put("floorNumber", sysDictDataService.selectDictDataByType("floor_number"));
        mmp.put("checkId", checkId);
        mmp.put("programName", programName);
        return prefix + "/plasterCheckEdit";
    }

    /**
     * 混凝土编辑页面:获取详细列表
     */
    @PostMapping("/detailListData")
    @ResponseBody
    public TableDataInfo detailListData(@RequestParam Map<String, Object> map) {
        startPage();
        List<Map<String, Object>> list = concretecheckService.selectConcretecheckDetailList(map);
        return getDataTable(list);
    }

    /**
     * 砌体编辑页面:获取详细列表
     */
    @PostMapping("/detailBrickData")
    @ResponseBody
    public TableDataInfo detailBrickData(@RequestParam Map<String, Object> map) {
        startPage();
        List<Map<String, Object>> list = concretecheckService.selectBrickcheckDetailList(map);
        return getDataTable(list);
    }

    /**
     * 抹灰编辑页面:获取详细列表
     */
    @PostMapping("/detailPlasterData")
    @ResponseBody
    public TableDataInfo detailPlasterData(@RequestParam Map<String, Object> map) {
        startPage();
        List<Map<String, Object>> list = concretecheckService.selectPlastercheckDetailList(map);
        return getDataTable(list);
    }

    /**
     * 抹灰编辑页面 > 保存按钮:保存报表数据
     *
     * @param concreteCheckParam
     *            混凝土报表封装实体类
     * @return 响应结果
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ConcreteCheckParam concreteCheckParam) {
        // Step1 参数准备
        List<ConcreteCheck> concreteCheckList = concreteCheckParam.getConcreteCheck();
        ConcreteCheck concreteCheck = concreteCheckList.get(0);
        concreteCheck.setCheckId(concreteCheckParam.getCheckId());

        // Step3 更新并响应结果
        return toAjax(concretecheckService.updateConcreteCheckById(concreteCheck));
    }

    /**
     * 砌体编辑页面 > 保存按钮:保存报表数据
     *
     * @param brickCheckParam
     *            砌体报表封装实体类
     * @return 响应结果
     */
    @PostMapping("/editBrick")
    @ResponseBody
    public AjaxResult editBrick(BrickCheckParam brickCheckParam) {
        // Step1 参数准备
        List<BrickCheck> list = brickCheckParam.getBrickCheck();
        BrickCheck brickCheck = list.get(0);
        brickCheck.setCheckId(brickCheckParam.getCheckId());

        // Step3 更新并响应结果
        return toAjax(concretecheckService.updateBrickCheckById(brickCheck));
    }

    /**
     * 砌体编辑页面 > 保存按钮:保存报表数据
     *
     * @param plasterCheckParam
     *            砌体报表封装实体类
     * @return 响应结果
     */
    @PostMapping("/editPlaster")
    @ResponseBody
    public AjaxResult editPlaster(PlasterCheckParam plasterCheckParam) {
        // Step1 参数准备
        List<PlasterCheck> list = plasterCheckParam.getPlasterCheck();
        PlasterCheck plasterCheck = list.get(0);
        plasterCheck.setCheckId(plasterCheckParam.getCheckId());

        // Step3 更新并响应结果
        return toAjax(concretecheckService.updatePlasterCheckById(plasterCheck));
    }

    /**
     * 删除按钮
     *
     * @param ids
     *            传入id
     * @return 响应结果
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(concretecheckService.deleteConcreteCheckBycheckId(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 砌体删除按钮
     *
     * @param ids
     *            传入id
     * @return 响应结果
     */
    @PostMapping("/removeBrick")
    @ResponseBody
    public AjaxResult removeBrick(String ids) {
        try {
            return toAjax(concretecheckService.deleteBrickCheckBycheckId(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 抹灰删除按钮
     *
     * @param ids
     *            传入id
     * @return 响应结果
     */
    @PostMapping("/removePlaster")
    @ResponseBody
    public AjaxResult removePlaster(String ids) {
        try {
            return toAjax(concretecheckService.deletePlasterCheckBycheckId(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 导出按钮
     *
     * @param map
     *            前端封装参数
     * @return 响应结果
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestParam Map<String, Object> map) {
        // Step1 获取需要导出的数据
        List<ConcreteCheck> list = concretecheckService.selectConcreteCheckList(map);
        String filePath = Global.getDownloadPath() + "混凝土实测统计表.xls";

        try {
            if (!ArrayUtil.isEmpty(list)) {
                // Step2 导出项目
                FileOutputStream fout = new FileOutputStream(filePath);
                new ExportConcreteExcel().getValue(list, fout);
                fout.close();
            }
        } catch (Exception e) {
            error(e.getMessage());
        }
        return AjaxResult.success(0);
    }

    /**
     * 导出按钮
     *
     * @param map
     *            前端封装参数
     * @return 响应结果
     */
    @PostMapping("/exportBrick")
    @ResponseBody
    public AjaxResult exportBrick(@RequestParam Map<String, Object> map) {
        // Step1 获取需要导出的数据
        List<BrickCheck> list = concretecheckService.selectBrickCheckList(map);
        String filePath = Global.getDownloadPath() + "砌体实测统计表.xls";

        try {
            if (!ArrayUtil.isEmpty(list)) {
                // Step2 导出多个项目
                FileOutputStream fout = new FileOutputStream(filePath);
                new ExportBrickExcel().getValue(list, fout);
                fout.close();
            }
        } catch (Exception e) {
            error(e.getMessage());
        }
        return AjaxResult.success(0);
    }

    /**
     * 导出按钮
     *
     * @param map
     *            前端封装参数
     * @return 响应结果
     */
    @PostMapping("/exportPlaster")
    @ResponseBody
    public AjaxResult exportPlaster(@RequestParam Map<String, Object> map) {
        // Step1 获取需要导出的数据
        List<PlasterCheck> list = concretecheckService.selectPlasterCheckList(map);
        String filePath = Global.getDownloadPath() + "抹灰实测统计表.xls";

        try {
            if (!ArrayUtil.isEmpty(list)) {
                // Step2 导出多个项目
                FileOutputStream fout = new FileOutputStream(filePath);
                new ExportPlasterExcel().getValue(list, fout);
                fout.close();
            }
        } catch (Exception e) {
            error(e.getMessage());
        }
        return AjaxResult.success(0);
    }

    /**
     * 导出所有按钮
     *
     * @param map
     *            前端封装参数
     * @return 响应结果
     */
    @PostMapping("/exportAll")
    @ResponseBody
    public AjaxResult exportAll(@RequestParam Map<String, Object> map) {
        // Step1 获取需要导出的数据
        getUserId(map);
        List<ConcreteCheck> concreteList = concretecheckService.selectConcreteCheckList(map);
        List<BrickCheck> brickcheckList = concretecheckService.selectBrickCheckList(map);
        List<PlasterCheck> plastercheckList = concretecheckService.selectPlasterCheckList(map);

        String filePath = Global.getDownloadPath() + "工艺实测统计表.xls";

        try {
            if (!ArrayUtil.isEmpty(concreteList)) {
                // Step2 导出多个项目
                FileOutputStream fout = new FileOutputStream(filePath);
                new ExportAllExcel().getValue(concreteList,brickcheckList,plastercheckList, fout);
                fout.close();
            }
        } catch (Exception e) {
            error(e.getMessage());
        }
        return AjaxResult.success(0);
    }

    /**
     * 导出所有工艺概要按钮
     *
     * @param map
     *            前端封装参数
     * @return 响应结果
     */
    @PostMapping("/exportOutline")
    @ResponseBody
    public AjaxResult exportOutline(@RequestParam Map<String, Object> map) {
        List<ConcreteCheck> list = concretecheckService.selectTotalList(map);
        String filePath = Global.getDownloadPath() + "工艺实测汇总统计表.xls";

        try {
            if (!ArrayUtil.isEmpty(list)) {
                // Step2 导出多个项目
                FileOutputStream fout = new FileOutputStream(filePath);
                new ExportOutlineExcel().getValue(list, fout);
                fout.close();
            }
        } catch (Exception e) {
            error(e.getMessage());
        }
        return AjaxResult.success(0);
    }
}
